<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>{{.category.Catname}} 内容发布</title>
    {{template "backend/public_javascript.html" . }}
    {{template "backend/public_umeditor.html" . }}
    <link rel="stylesheet" type="text/css" href="{{.staticDir}}/jquery.tagsinput.css"/>
    <script src="{{.staticDir}}/jquery.tagsinput.js"></script>
    <script src="{{.staticDir}}/jquery.upload.js"></script>
    <link href="{{.staticDir}}/css/bootstrap.min.css" rel="stylesheet">
    <link href="{{.staticDir}}/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="{{.staticDir}}/css/morris.css">
    <link href="{{.staticDir}}/css/minoral.css" rel="stylesheet">
    <style>
        .btn {
            padding: 5px 12px;
        }
    </style>
    <script>
        let editors = [];
        let siginUploader = [];
        let MultiUploader = [];
        let tagger = [];
        let fileUploader = [];
    </script>
</head>
<script type="text/javascript">
    if (parent.ajaxLoadEnd) {
        parent.ajaxLoadEnd()
    }
    function fromUEImageUploader(that) {
        event.preventDefault();
        var myEditorImage = UE.getEditor('myEditorImage', { autoHeightEnabled: false, isShow: false });
        myEditorImage.ready(function () {
            myEditorImage.addListener('beforeInsertImage', function (t, arg) {
                $(that).attr("src", arg[0].src)
                $(that).next().val(arg[0].src)
            });
            setTimeout(function () {
                var d = myEditorImage.getDialog("insertimage");
                d.render();
                d.open();
            }, 200)
        });
        return false
    }

    function fromUEFileUploader(that) {
        var myEditorImage = UE.getEditor('myEditorImage', {autoHeightEnabled: false, isShow: false});
        myEditorImage.ready(function () {
            myEditorImage.addListener('beforeInsertFile', function (t, args) {
                var vals = $(that).prev().val();
                var urlSet = new Set();
                if (vals) {
                    vals = JSON.parse(vals)    // 序列化数据字符串
                } else {
                    vals = [];
                }
                vals = [...new Set(vals.concat(args))].filter(item => {
                    if (urlSet.has(item.url) ) {
                        return false;
                    }  else {
                        urlSet.add(item.url);
                        return true;
                    }
                });
                $(that).next().find(".list").html("");
                buildFileLists(that, vals)
                $(that).prev().val(JSON.stringify(vals))    // 序列化数据字符串
            });

            setTimeout(function () {
                var d = myEditorImage.getDialog("attachment");
                d.render();
                d.open();
            }, 200)
        });
        return false
    }

    function buildFileLists(that, vals) {
        for (var i = 0; i < vals.length; i++) {
            $(that).next().find(".list").append(`<li class="list-item">
                        <i class="file-preview `+'file-type-' + vals[i].type +`"></i>
                        <div class="info"><p class="filename">`+vals[i].title+`</p></div>
                        <label class="btn-delete-item delete-label cursor-select" onclick="removeFileLi(this, '`+vals[i].title+`', '`+vals[i].url+`')"><i class="delete-icon">X</i></label>
                        </li>`)
        }
    }

    function removeFileLi(obj, title, url) {
        $.messager.confirm("删除提醒", "是否要删除文件附件: " + title, function (res) {
            if (res){
                $(obj).parents(".easy-uploader").prev().prev().val(JSON.stringify( JSON.parse($(obj).parents(".easy-uploader").prev().prev().val()).filter(item => item.url !== url)));
                $(obj).parent().remove()
            }
        })
    }

    function contentPageFormSubmit() {
        $.post('{{.submitURL}}?catid={{.category.Catid}}'.replace("\\/", "/"), $("#content_page_form").serialize(), function (res) {
            if (res.errcode) {
                parent.$.messager.alert('提示信息', res.errmsg, 'error');
            } else {
                parent.$.messager.alert('提示信息', res.errmsg, 'info');
                // 刷新
                parent.$("#category_newslist_datagrid").datagrid("reload");
            }
        });
        return false;
    }

    function doUpload(obj) {
        event.preventDefault();
        {{if eq .preview 1}}
        $.messager.alert("错误提醒", "当前处于预览模式", "error");
        return;
        {{end}}
        $.upload({
            url: "/public/upload",
            fileName: 'filedata',
            params: {},
            dataType: 'json',
            onSend: function () {
                return true;
            },
            onComplate: function (data) {
                if (data.errcode == 1) {
                    $.messager.alert("错误提醒", data.errmsg, "error")
                } else {
                    $(obj).next().val(data.url);
                    $(obj).attr('src', data.url);
                }
            }
        });
        return false;
    }

    function createHtml(obj, field, maxNumInt) {
        if (maxNumInt > 0 && $(obj).parent().find(".imgbox").size() >= maxNumInt + 1) {
            $.messager.alert("提醒", "图片限制数量已达上限" + maxNumInt + ",无法添加", "info");
            return false;
        }
        var str = "<div class='imgbox'>\
                  <input class='imgbox_inputBtn' type='image' onclick='return doUpload(this)' src='/assets/backend/static/nopic.jpg' alt='点击上传'/>\
                  <input type='hidden' name='" + field + "' />\
                  <span title=\"删除图片框\" style='color:#fff;display:inline-block;width:15px;height:15px;font-size:15px;line-height:15px;text-align:center;background:rgba(0,0,0,0.5);font-weight:normal;cursor:pointer;    position: absolute;left: 72px;top: 10px;'  onclick='delImgBox(this)'>×</span>\
        </div>";
        $(obj).before(str);
        return false;
    }

    function DelImg(obj) {
        $.messager.confirm('清空提醒', '要清空此图片吗？', function (r) {
            if (r) {
                $(obj).prev().val("");
                $(obj).prev().prev().attr('src', "/assets/backend/static/nopic.jpg");
            }
        });
    }

    function delImgBox(obj) {
        $.messager.confirm('删除提醒', '要删除此图片吗？', function (r) {
            if (r) {
                $(obj).parents(".imgbox").remove();
            }
        });
    }

    function submitForm() {
        {{if eq .preview 1}}
        $.messager.alert("错误提醒", "当前处于预览模式", "error");
        return;
        {{end}}
        $('#content_page_form').form('submit', {
            onSubmit: function () {
                for (let i in siginUploader) {
                    if (!siginUploader[i]()) return false;
                }
                for (let i in MultiUploader) {
                    if (!MultiUploader[i]()) return false;
                }
                for (let i in editors) {
                    if (!editors[i]()) return false;
                }
                for (let i in tagger) {
                    if (!tagger[i]()) return false;
                }
                return $(this).form('enableValidation').form('validate');
            },
            success: function (data) {
                if (typeof data === "string") {
                    console.log(data)
                    data = JSON.parse(data)
                }
                if (!data.errcode) {
                    $.messager.alert("提醒", data.errmsg, "info");
                    parent.$("#category_newslist_datagrid").datagrid("reload");
                    // 关闭
                    parent.$("#publish_content").dialog("close");
                } else {
                    $.messager.alert("提醒", data.errmsg, "error");
                }
            }
        });
    }

</script>
<body>
<div>
    {{.form}}
</div>

<div id="myEditorImage" style="display: none"></div>
</body>
</html>
